package com.codeoss.module.purchase.service.impl;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.framework.util.DateUtil;
import com.codeoss.module.ir.service.IIrDocTypeService;
import com.codeoss.module.ir.service.IIrSequenceService;
import com.codeoss.module.purchase.dao.PurchaseReturnMapper;
import com.codeoss.module.purchase.model.PurchaseOrderLine;
import com.codeoss.module.purchase.model.PurchaseReturn;
import com.codeoss.module.purchase.model.PurchaseReturnLine;
import com.codeoss.module.purchase.service.IPurchaseOrderLineService;
import com.codeoss.module.purchase.service.IPurchaseReturnLineService;
import com.codeoss.module.purchase.service.IPurchaseReturnService;
import com.codeoss.module.stock.model.StockIn;
import com.codeoss.module.stock.model.StockInLine;
import com.codeoss.module.stock.service.IStockInLineService;
import com.codeoss.module.stock.service.IStockInService;

@Service
public class PurchaseReturnService extends BaseService<PurchaseReturnMapper, PurchaseReturn>
		implements IPurchaseReturnService,ApplicationContextAware {
	@Autowired
	private IPurchaseReturnLineService purchaseReturnLineService;
	private ApplicationContext applicationContext;
	@Autowired
	private IIrDocTypeService irDocTypeService;
	@Autowired
	private DateUtil dateUtil;
	@Autowired
	private IIrSequenceService irSequenceService;
	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		List<PurchaseReturnLine> lineList=purchaseReturnLineService.getLineListByPurchaseReturnId(id);
		if(lineList==null ||lineList.size()==0 ) {
			throw new RuntimeException("没有行明细");
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		PurchaseReturn purchaseReturn=getById(id);
		purchaseReturn.setApproved(true);
		update(purchaseReturn);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		PurchaseReturn purchaseReturn=getById(id);
		purchaseReturn.setApproved(false);
		update(purchaseReturn);
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		PurchaseReturn purchaseReturn=getById(id);
		purchaseReturn.setDocStatus("CO");
		update(purchaseReturn);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id, String docStatus) {
		// TODO Auto-generated method stub
		PurchaseReturn purchaseReturn=getById(id);
		purchaseReturn.setDocStatus(docStatus);
		update(purchaseReturn);
	}

	@Override
	@Transactional
	public PurchaseReturn generatePurchaseReturn(Long stockInId) {
		// TODO Auto-generated method stub
		IStockInService stockInService=applicationContext.getBean(IStockInService.class);
		IStockInLineService stockInLineService=applicationContext.getBean(IStockInLineService.class);
		StockIn stockIn=stockInService.getById(stockInId);
		String docStatus=stockIn.getDocStatus();
		if(!"CO".equals(docStatus)) {
			throw new RuntimeException("请先完成单据");
		}
		
		PurchaseReturn purchaseReturn=new PurchaseReturn();
		Long docTypeId=irDocTypeService.getIrDocTypeIdByCode("purchasereturn");
		purchaseReturn.setIrDocTypeId(docTypeId);
		purchaseReturn.setResPartnerId(stockIn.getResPartnerId());
		purchaseReturn.setStockInId(stockInId);
		purchaseReturn.setDocStatus("DR");
		purchaseReturn.setDateReturn(dateUtil.getSqlNowDatetime());
		purchaseReturn.setDocumentNo(irSequenceService.getNextSequenceNo("purchasereturn"));
		insert(purchaseReturn);
		
		List<StockInLine> stockInLineList=stockInLineService.getLineListByStockInId(stockInId);
		IPurchaseOrderLineService purchaseOrderLineService=applicationContext.getBean(IPurchaseOrderLineService.class);
		for(StockInLine stockInLine:stockInLineList) {
			PurchaseReturnLine purchaseReturnLine=new PurchaseReturnLine();
			purchaseReturnLine.setStockInLineId(stockInLine.getId());
			purchaseReturnLine.setProductProductId(stockInLine.getProductProductId());
			purchaseReturnLine.setProductAttrSetInstanceId(stockInLine.getProductAttrSetInstanceId());
			purchaseReturnLine.setUomUomId(stockInLine.getUomUomId());
			purchaseReturnLine.setQtyEntered(stockInLine.getQtyEntered());
			Long purchaseOrderLineId=stockInLine.getPurchaseOrderLineId();
			PurchaseOrderLine purchaseOrderLine=purchaseOrderLineService.getById(purchaseOrderLineId);
			purchaseReturnLine.setAccountTaxId(purchaseOrderLine.getAccountTaxId());
			purchaseReturnLine.setQtyReturn(purchaseOrderLine.getQtyOrdered());
			purchaseReturnLine.setAmountSubtotal(purchaseOrderLine.getAmountSubtotal());
			purchaseReturnLine.setDiscount(purchaseOrderLine.getDiscount());
			purchaseReturnLine.setPurchaseReturnId(purchaseReturn.getId());
			purchaseReturnLineService.insert(purchaseReturnLine);
		}
		return purchaseReturn;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}
	
}